<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\cloud_market\controller\api\developer;

use addons\cloud_market\controller\api\CloudMarketApiController;
use addons\cloud_market\model\CloudMarketAddonSku;
use addons\cloud_market\model\CloudMarketAddonVersions;
use addons\cloud_market\model\CloudMarketSite;
 use hiduoke\services\FormBuilder as Form;

class Package extends CloudMarketApiController
{
    protected $notApiCheck = ['*'];

    public function initialize()
    {
        parent::initialize();
        $this->model = new CloudMarketAddonSku();
        $this->Site = new CloudMarketSite();
        $this->version_model = new CloudMarketAddonVersions();
    }

    /**
     * 【商城】获取列表
     */
    public function list()
    {
        $aid = $this->request->post('aid', 0);
        $mix = ['where' => ['aid', '=', $aid]];
        $res = $this->model->getPageList($mix);
        return $this->success($res['count'], $res['data']);
    }

    public function getForm($id, $aid)
    {
        $row = $this->model->findOrEmpty($id);//创建表单
        $action = $this->request->domain() . "/api.developer.package/save?id=" . $id;
        $form = Form::createForm($action)->setMethod("POST");
        $pic_images = explode(',', $row['pic_images']);
        // 循环
        foreach ($pic_images as $key => $value) {
            $pic_images[$key] = cdnurl($value);
        }
        //新增表单
        $rule = [
            Form::hidden('aid', $aid),
            Form::input('name', '名称', $row['name'] ?? '')->required(),
            Form::dkInputNumber('weigh', '排序', $row['weigh'] ?? '')->required(),
            Form::dkInputNumber('service', '服务期限', $row['service'] ?? '')->required(),
            Form::dkInputNumber('count', '激活平台数', $row['count'] ?? '')->required(),
            Form::dkInputNumber('price', '价格', $row['price'] ?? '')->required(),
            Form::textarea('desc', '描述', $row['desc'] ?? '')->required(),
            Form::radio('status', '状态', $row['status'] ?? 0)
                ->options([['label' => '正常', 'value' => '0'], ['label' => '禁用', 'value' => '-1']]),
        ];
        $title = !$id ? "新增" : "编辑";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function save($id = 0)
    {
        $row = $this->model->findOrEmpty($id);
        $formData = getFormData();
        $row->save($formData);
        return $this->success("保存成功");
    }

    public function del()
    {
       return $this->error('禁止删除');
        $id = input('id', 0);
        $row = $this->model->findOrEmpty($id);
        $row->delete();
        return $this->success("删除成功");
    }
}
